package com.aivin.popuwindowdemo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.PopupWindow;

import androidx.appcompat.app.AppCompatActivity;
import com.aivin.popuwindowdemo.callback.IWindowCallback;
import com.aivin.popuwindowdemo.tool.WkLogTool;
import com.aivin.popuwindowdemo.window.DeviceSettingWindow;
import com.aivin.popuwindowdemo.window.WxPicDownWindow;

public class MainActivity extends AppCompatActivity {
    private Context context ;
    private View bt_click ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = this ;
        bt_click =findViewById(R.id.bt_click);
        bt_click .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow();
            }
        });


        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){

                    if(popupWindow==null){
                       continue;
                    }
                    bt_click.post(new Runnable() {
                        @Override
                        public void run() {
                            popupWindow.updateDeviceTimeZone("时区_"+System.currentTimeMillis());
                        }
                    }) ;


                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start(); ;
    }

    private DeviceSettingWindow popupWindow ;
    /**
     * 一级弹窗
     */
    private void showPopupWindow() {
        if(popupWindow==null){
            popupWindow = new DeviceSettingWindow(context ,iWindowCallback);
        }
        popupWindow.showAtLocation( getContentView(MainActivity.this),  Gravity.CENTER, 0, 0);
    }


    private IWindowCallback iWindowCallback = new IWindowCallback() {
        @Override
        public void toCheckWxPushBind() {
            // 二级弹窗
            WxPicDownWindow popupWindow = new WxPicDownWindow(context );
            popupWindow.showAtLocation( getContentView(MainActivity.this),  Gravity.CENTER, 0, 0);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    WkLogTool.showLog("二级弹窗关闭，需要去下载图片");
                }
            });
        }
    } ;



    private View getContentView(Activity activity) {
        ViewGroup view = (ViewGroup) activity.getWindow().getDecorView();
        FrameLayout content = view.findViewById(android.R.id.content);
        return content.getChildAt(0);
    }
}
